package com.sf.ams.saxhandler;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import com.sf.ams.constant.Constant;

/**
 * 描述：
 * 
 * <pre>HISTORY
 * ****************************************************************************
 *  ID   DATE           PERSON          REASON
 *  1    2017年10月27日      Simba.Hua         Create
 * ****************************************************************************
 * </pre>
 * @author Simba.Hua
 */
@Component
public class DataStoreManager {
	@Autowired
	private ResourceLoader resourceLoader;
	
	private DataStoreManager(){
		
	}
	private static class Singleton{
		private static DataStoreManager instance;
		static{
			instance = new DataStoreManager();
		}
		public static DataStoreManager getInstance(){
			return instance;
		}
	}
	public static DataStoreManager getInstance(){
		return Singleton.getInstance();
	}
	public File getDataStoreXml() throws IOException {
		File localDataStore = new File(getDataStoreXmlPath());
		if(localDataStore.exists()){
			return localDataStore;
		}
		File parentFile = new File(Constant.LOCAL_DATA_STORE_DIR);
		if(parentFile.exists()) {
			parentFile.delete();
		}
		parentFile.mkdirs();
		InputStream inputStream = new ClassPathResource(Constant.DATASTOREDIR).getInputStream();
		FileUtils.copyInputStreamToFile(inputStream, localDataStore);
		return localDataStore;
	}
	public String getDataStoreXmlPath(){
		return Constant.LOCAL_DATA_STORE_DIR + Constant.PATH_SPLIT_CHARACTOR + Constant.DATASTOREDIR;
	}
}
